package com.example.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.chapter03.util.DateUtil;

public class ButtonClickActivity extends AppCompatActivity implements View.OnClickListener{
    private TextView tv_results;
    private TextView tv_result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_button_click);
        tv_results = findViewById(R.id.tv_results);
        tv_result = findViewById(R.id.tv_result);
        Button btn_click_single =  findViewById(R.id.btn_click_single);
        btn_click_single.setOnClickListener(new MyOnClickListener(tv_results));
        Button btn_click_public = findViewById(R.id.btn_click_public);
        btn_click_public.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_click_public){
            String desc = String.format("%s 您点击了按钮：    %s",
                    DateUtil.getNowTime(),((Button) view).getText());
            tv_result.setText(desc);
        }
    }

    static class MyOnClickListener implements View.OnClickListener {

        private TextView tv_results;

        public MyOnClickListener(TextView tv_results) {
            this.tv_results = tv_results;
        }

        @Override
        public void onClick(View view) {
                String desc = String.format("%s 您点击了按钮：    %s",
                        DateUtil.getNowTime(),((Button) view).getText());
            tv_results.setText(desc);

        }



    }
}